package dao;

import java.util.List;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import model.Admin;
import model.Tourist;
import util.EntityManagerUtil;

public class LoginDao {
	public static boolean loginCheckByTourist(String email,String password) {
		try {
			EntityManager em = EntityManagerUtil.getEntityManager();
			Query query = em.createQuery("SELECT c FROM Tourist c where c.email = :email and c.password =:password");
			query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
			Tourist tourist  = (Tourist) query.setParameter("email", email ).setParameter("password", password).getSingleResult();
			em.close();
			System.out.println(tourist.getEmail());
			return true;
		} catch (Exception e) {
			//e.printStackTrace();
			return false;
		}
	}
	public static boolean loginCheckByAdmin(String email,String password) {
		try {
			EntityManager em = EntityManagerUtil.getEntityManager();
			Query query = em.createQuery("SELECT c FROM Admin c where c.id = :email and c.password =:password");
			query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
			Admin admin  = (Admin) query.setParameter("email", email ).setParameter("password", password).getSingleResult();
			//System.out.println(admin.getId());
			em.close();
			return true;
		} catch (Exception e) {
			//e.printStackTrace();
			return false;
		}
	}
	public static void main(String args[]) {
		String phone = "15628931723";
		System.out.println(phone);
		String p1 = phone.substring(1,7);
		String p2 = phone.substring(7,11);
		int ph1 = Integer.parseInt(p1);
		int ph2 = Integer.parseInt(p2);
		
		System.out.println(ph1);
		System.out.println(ph2);
		
		int phInt = ph1*10000+ph2;
		System.out.println(ph1*10000);
		System.out.println(phInt);
	}
}
